package io.github.talelin.latticy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import io.github.talelin.latticy.dto.zty.ShopListDTO;
import io.github.talelin.latticy.dto.zty.child.ShopListDetailDTO;
import io.github.talelin.latticy.mapper.*;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.pojo.*;
import io.github.talelin.latticy.service.ReplenishListService;
import io.github.talelin.latticy.vo.zty.ReplenishDetailVO;
import io.github.talelin.latticy.vo.zty.ReplenishListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZTY
 * @since 2021-04-15
 */
@Service
@Transactional
public class ReplenishListServiceImpl extends ServiceImpl<ReplenishListMapper, ReplenishList> implements ReplenishListService {

    @Autowired
    ReplenishListMapper replenishListMapper;

    @Autowired
    ReplenishDetailMapper replenishDetailMapper;

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    GoodsMapper goodsMapper;


    @Override
    public int createReplenishList(ShopListDTO shopListDTO) {
        ReplenishList replenishList = new ReplenishList();
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();
        shopQueryWrapper.eq("shop_name", shopListDTO.getShopName());
        Shop shop = shopMapper.selectOne(shopQueryWrapper);
        QueryWrapper<UserDO> userDOQueryWrapper = new QueryWrapper<>();
        userDOQueryWrapper.eq("username", shopListDTO.getUsername());
        UserDO userDO = userMapper.selectOne(userDOQueryWrapper);
        replenishList.setShopId(shop.getShopId());
        replenishList.setUserId(userDO.getId());
        replenishList.setStatus(1);
        int insert = replenishListMapper.insert(replenishList);
        List<ShopListDetailDTO> shopListDetail = shopListDTO.getShopListDetails();
        for (ShopListDetailDTO shopListDetailDTO : shopListDetail) {
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
            goodsQueryWrapper.eq("goods_name", shopListDetailDTO.getGoodsName());
            Goods goods = goodsMapper.selectOne(goodsQueryWrapper);
            ReplenishDetail replenishDetail = new ReplenishDetail();
            replenishDetail.setGoodsId(goods.getGoodsId());
            replenishDetail.setQuantity(shopListDetailDTO.getNumber());
            replenishDetail.setReplenishId(replenishList.getReplenishId());
            int insert1 = replenishDetailMapper.insert(replenishDetail);
        }

        return insert;
    }

    @Override
    public List<ReplenishListVO> getReplenishLists() {
        QueryWrapper<ReplenishList> replenishListQueryWrapper = new QueryWrapper<>();
        replenishListQueryWrapper.orderByAsc("status").orderByDesc("create_time");
        List<ReplenishList> replenishLists = replenishListMapper.selectList(replenishListQueryWrapper);
        List<ReplenishListVO> replenishListVOs = new ArrayList<>();
        for (ReplenishList replenishList : replenishLists) {
            Shop shop = shopMapper.selectById(replenishList.getShopId());
            UserDO userDO = userMapper.selectById(replenishList.getUserId());
            ReplenishListVO replenishListVO = new ReplenishListVO();
            replenishListVO.setCreateTime(replenishList.getCreateTime());
            replenishListVO.setReplenishId(replenishList.getReplenishId());
            replenishListVO.setShopName(shop.getShopName());
            replenishListVO.setUsername(userDO.getUsername());
            if (replenishList.getStatus() == 1){
                replenishListVO.setStatus("待出库");
            }else if (replenishList.getStatus() == 2){
                replenishListVO.setStatus("审核完毕，已出库");
            }else{
                replenishListVO.setStatus("被驳回");
            }
            replenishListVOs.add(replenishListVO);
        }
        return replenishListVOs;
    }

    @Override
    public List<ReplenishDetailVO> getReplenishDetailsByListId(int listId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("replenish_id",listId);
        List<ReplenishDetail> replenishDetails = replenishDetailMapper.selectByMap(map);
        List<ReplenishDetailVO> replenishDetailVOs = new ArrayList<>();
        for (ReplenishDetail replenishDetail : replenishDetails) {
            ReplenishDetailVO replenishDetailVO = new ReplenishDetailVO();
            Goods goods = goodsMapper.selectById(replenishDetail.getGoodsId());
            replenishDetailVO.setGoodsName(goods.getGoodsName());
            replenishDetailVO.setQuantity(replenishDetail.getQuantity());
            replenishDetailVO.setReplenishDetailId(replenishDetail.getReplenishDetailId());
            replenishDetailVO.setReplenishId(replenishDetail.getReplenishId());
            replenishDetailVO.setGoodsPrice(goods.getGoodsPrice());
            replenishDetailVOs.add(replenishDetailVO);
        }
        return replenishDetailVOs;
    }

}
